<template>
  <div :style="{ ...coreStyle.marginSame(8) }">
    <a-card>
      <a-row :gutter="12">
        <a-col :span="12">
          <a-space>
            <a-button type="primary" ghost @click="orgAdd">
              <CoreIcon icon="carbon:add-alt" :size="14" />
              新增
            </a-button>
            <a-button type="dashed" @click="orgUpdate" v-if="orgTreeCurrentSelectData.length === 1">
              <CoreIcon icon="ri:brush-line" :size="14" />
              修改
            </a-button>
            <a-popconfirm title="确认删除？" placement="bottom" @confirm="orgDelete" v-if="orgTreeCurrentSelectData.length === 1">
              <a-button type="dashed" danger>
                <CoreIcon icon="jam:delete" :size="14" />
                删除
              </a-button>
            </a-popconfirm>
            <a-button @click="orgDetailInfo" v-if="orgTreeCurrentSelectData.length === 1">
              <CoreIcon icon="heroicons-outline:light-bulb" :size="14" />
              详情
            </a-button>
          </a-space>
          <a-space :style="{ ...coreStyle.floatRight }">
            <a-button type="link" shape="circle" @click="orgTreeGetDataSet">
              <CoreIcon icon="ci:refresh" :size="22" />
            </a-button>
          </a-space>
          <a-card :bordered="false" :bodyStyle="{ border: '1px dashed #0000000f' }" :style="{ ...coreStyle.marginTop(10) }">
            <a-row>
              <a-col :span="6">
              </a-col>
              <a-col :span="18">
                <div :style="{ ...coreStyle.height(598) }">
                  <ScrollContainer>
                    <a-tree v-model:selectedKeys="orgTreeCurrentSelectData" :tree-data="orgTreeData" defaultExpandAll @select="orgTreeChangeSelect" v-if="orgTreeData.length">
                    </a-tree>
                  </ScrollContainer>
                </div>
              </a-col>
            </a-row>
          </a-card>
        </a-col>
        <a-col :span="12">
          <a-space>
            <a-button type="primary" ghost :disabled="orgTreeCurrentSelectData.length !== 1" @click="postAdd">
              <CoreIcon icon="carbon:add-alt" :size="14" />
              新增
            </a-button>
            <a-button type="dashed" @click="postUpdate" v-if="postListSelectLength === 1">
              <CoreIcon icon="ri:brush-line" :size="14" />
              修改
            </a-button>
            <a-popconfirm title="确认删除？" placement="bottom" @confirm="postDelete" v-if="postListSelectLength > 0">
              <a-button type="dashed" danger>
                <CoreIcon icon="jam:delete" :size="14" />
                删除
              </a-button>
            </a-popconfirm>
            <a-button @click="postDetailInfo" v-if="postListSelectLength === 1">
              <CoreIcon icon="heroicons-outline:light-bulb" :size="14" />
              详情
            </a-button>
          </a-space>
          <a-space :style="{ ...coreStyle.floatRight }">
            <a-button type="link" shape="circle" @click="postFormQuerySearch">
              <CoreIcon icon="ci:refresh" :size="22" />
            </a-button>
          </a-space>
          <a-table
            rowKey="id"
            :columns="postListColumns"
            :data-source="postListDataSet"
            :loading="postListLoading"
            :pagination="false"
            bordered
            :customRow="postListCustomRow"
            :row-selection="{ fixed: true, selectedRowKeys: postListSelectedRowKeys, onChange: postListRowOnChange }"
            :scroll="{ x: '100%', y: 550 }"
            :style="{ ...coreStyle.marginTop(10) }"
          >
          </a-table>
          <a-pagination
            v-model:current="postListDataPage"
            v-model:pageSize="postListDataRows"
            :total="postListDataCount"
            show-size-changer
            show-quick-jumper
            :page-size-options="[ ...coreStyle.antdPaginationPageSizeOptions ]"
            :show-total="(total, range) => coreStyle.antdPaginationShowTotal(total, range)"
            :style="{ ...coreStyle.marginTop(10), ...coreStyle.textAlignCenter }"
            @change="postListPaginationChange"
            @showSizeChange="postListPaginationShowSizeChange"
            v-if="postListPaginationVisible"
          >
          </a-pagination>
          <p :style="{ ...coreStyle.marginTop(14), ...coreStyle.marginBottom(6), ...coreStyle.textAlignCenter }" v-if="!postListPaginationVisible">
            {{ coreStyle.antdPaginationShowTotal(postListDataCount) }}
          </p>
        </a-col>
      </a-row>
    </a-card>
    <a-drawer
      v-model:visible="orgEditDrawerVisible"
      title="编辑组织机构信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="orgEditDrawerOnClose"
    >
      <a-form ref="orgFormRef" :model="orgFormModel" :rules="orgFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="24">
            <a-form-item label="上级组织机构" name="pid">
              <!-- <a-input v-model:value="orgFormModel.pid" placeholder="输入父级Id" /> -->
              <a-select v-model:value="orgFormModel.pid" optionFilterProp="title" show-search allowClear placeholder="请选择" :disabled="orgFormModel.id" :style="{ ...coreStyle.width('100%') }">
                <a-select-option v-for="item in orgPidSelectData" :value="item.id" :key="item.id" :title="item.orgName + ' - ' + item.orgNameAbbr">
                  {{ item.orgName + ' - ' + item.orgNameAbbr }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="组织机构名称" name="orgName">
              <a-input v-model:value="orgFormModel.orgName" placeholder="输入组织机构名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="组织机构简称" name="orgNameAbbr">
              <a-input v-model:value="orgFormModel.orgNameAbbr" placeholder="输入组织机构简称" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <!-- <a-form-item label="组织机构类型" name="orgType">
              <a-input v-model:value="orgFormModel.orgType" placeholder="输入组织机构类型" />
            </a-form-item> -->
            <Dict v-model="orgFormModel.orgType" dictKey="orgType" formItemName="orgType" />
          </a-col>
          <a-col :span="24">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="orgFormModel.weightOrder" :style="{ ...coreStyle.width(200) }" />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="12">
            <a-form-item label="Id路径" name="idPath">
              <a-input v-model:value="orgFormModel.idPath" placeholder="输入Id路径" />
            </a-form-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-item label="描述" name="description">
              <a-textarea v-model:value="orgFormModel.description" :rows="3" placeholder="输入描述" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button @click="orgFormReset">
            <CoreIcon icon="ant-design:clear-outlined" :size="14" />
            重置
          </a-button>
          <a-button type="primary" ghost :loading="orgFormSaveSubmitting" @click="orgFormNextAdd">
            <CoreIcon icon="tabler:player-track-next" :size="14" />
            继续录入
          </a-button>
          <a-button type="primary" ghost :loading="orgFormSaveSubmitting" @click="orgFormSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="orgDetailDrawerVisible"
      title="组织机构详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <a-descriptions-item label="组织机构名称" :span="1">{{ orgDetailModel.orgName }}</a-descriptions-item>
          <a-descriptions-item label="组织机构简称" :span="1">{{ orgDetailModel.orgNameAbbr }}</a-descriptions-item>
          <a-descriptions-item label="组织机构类型" :span="1">{{ coreToolStore.getDictDisplayName('orgType', orgDetailModel.orgType) }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="1">{{ orgDetailModel.weightOrder }}</a-descriptions-item>
          <!-- <a-descriptions-item label="Id路径" :span="1">{{ orgDetailModel.idPath }}</a-descriptions-item> -->
          <a-descriptions-item label="描述" :span="2">{{ orgDetailModel.description }}</a-descriptions-item>
          <!-- <a-descriptions-item label="父级Id" :span="1">{{ orgDetailModel.pid }}</a-descriptions-item> -->
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" @click="orgDetailUpdate">
            <CoreIcon icon="ri:brush-line" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="postEditDrawerVisible"
      title="编辑岗位信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
      @close="postEditDrawerOnClose"
    >
      <a-form ref="postFormRef" :model="postFormModel" :rules="postFormRules" labelAlign="left" layout="vertical">
        <a-row :gutter="16">
          <a-col :span="24">
            <a-form-item label="所属组织机构" name="fkid">
              <!-- <a-input v-model:value="postFormModel.fkid" placeholder="输入外键Id" /> -->
              <a-select v-model:value="postFormModel.fkid" optionFilterProp="title" show-search allowClear disabled placeholder="请选择" :style="{ ...coreStyle.width('100%') }">
                <a-select-option v-for="item in postFkidSelectData" :value="item.id" :key="item.id" :title="item.orgName + ' - ' + item.orgNameAbbr">
                  {{ item.orgName + ' - ' + item.orgNameAbbr }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="岗位名称" name="postName">
              <a-input v-model:value="postFormModel.postName" placeholder="输入岗位名称" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="排序权重" name="weightOrder">
              <a-input-number v-model:value="postFormModel.weightOrder" :style="{ ...coreStyle.width('100%') }" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="角色列表" name="roleList">
              <a-select v-model:value="postFormModel.roleList" mode="multiple" optionFilterProp="title" show-search allowClear placeholder="请选择" :style="{ ...coreStyle.width('100%') }">
                <a-select-option v-for="item in postRoleListSelectData" :value="item.id" :key="item.id" :title="item.roleName + ' - [' + coreToolStore.getDictDisplayName('roleType', item.roleType) + ']'">
                  {{ item.roleName + ' - [' + coreToolStore.getDictDisplayName('roleType', item.roleType) + ']' }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="描述" name="description">
              <a-textarea v-model:value="postFormModel.description" :rows="3" placeholder="输入描述" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button @click="postFormReset">
            <CoreIcon icon="ant-design:clear-outlined" :size="14" />
            重置
          </a-button>
          <a-button type="primary" ghost :loading="postFormSaveSubmitting" @click="postFormNextAdd">
            <CoreIcon icon="tabler:player-track-next" :size="14" />
            继续录入
          </a-button>
          <a-button type="primary" ghost :loading="postFormSaveSubmitting" @click="postFormSave">
            <CoreIcon icon="ant-design:save-outlined" :size="14" />
            保存
          </a-button>
        </a-space>
      </div>
    </a-drawer>
    <a-drawer
      v-model:visible="postDetailDrawerVisible"
      title="岗位详细信息"
      placement="right"
      :closable="false"
      :width="'40%'"
      :header-style="{ ...coreStyle.antdDrawerHeader }"
      :body-style="{ ...coreStyle.margin(50, 0, 30, 0) }"
    >
      <a-space direction="vertical" :size="20" :style="{ ...coreStyle.width('100%') }">
        <a-descriptions title="基础信息" :column="2" bordered>
          <a-descriptions-item label="岗位名称" :span="1">{{ postDetailModel.postName }}</a-descriptions-item>
          <a-descriptions-item label="排序权重" :span="1">{{ postDetailModel.weightOrder }}</a-descriptions-item>
          <a-descriptions-item label="描述" :span="2">{{ postDetailModel.description }}</a-descriptions-item>
          <!-- <a-descriptions-item label="外键Id" :span="1">{{ postDetailModel.fkid }}</a-descriptions-item> -->
        </a-descriptions>
      </a-space>
      <div :style="{ ...coreStyle.antdDrawerFooter }">
        <a-space>
          <a-button type="dashed" @click="postDetailUpdate">
            <CoreIcon icon="ri:brush-line" :size="14" />
            修改
          </a-button>
        </a-space>
      </div>
    </a-drawer>
  </div>
</template>

<script lang="ts">
import { Options, Vue } from 'vue-class-component'
import { mapStores } from 'pinia'
import { useCoreToolStore } from '/@/store/modules/cn/core/tool/coreToolStore'
import type { Res } from '/@/tool/Res'
import type { Param } from '/@/tool/Param'
import type { CoreTree } from '/@/tool/CoreTree'

import { useOrgStore } from '/@/store/modules/cn/core/sign/orgStore'

import type { Org } from '/@/model/cn/core/sign/eo/Org'
import { toOrg } from '/@/model/cn/core/sign/eo/Org'

import type { OrgQO } from '/@/model/cn/core/sign/qo/OrgQO'
import { toOrgQO } from '/@/model/cn/core/sign/qo/OrgQO'

import type { OrgVO } from '/@/model/cn/core/sign/vo/OrgVO'
import { toOrgVO } from '/@/model/cn/core/sign/vo/OrgVO'

import { usePostStore } from '/@/store/modules/cn/core/sign/postStore'

import type { Post } from '/@/model/cn/core/sign/eo/Post'
import { toPost } from '/@/model/cn/core/sign/eo/Post'

import type { PostQO } from '/@/model/cn/core/sign/qo/PostQO'
import { toPostQO } from '/@/model/cn/core/sign/qo/PostQO'

import type { PostVO } from '/@/model/cn/core/sign/vo/PostVO'
import { toPostVO } from '/@/model/cn/core/sign/vo/PostVO'

import { ScrollContainer } from '/@/components/Container/index'

import { useRoleStore } from '/@/store/modules/cn/core/sign/roleStore'

import type { Role } from '/@/model/cn/core/sign/eo/Role'
import type { RoleQO } from '/@/model/cn/core/sign/qo/RoleQO'

@Options({
  name: 'CoreOrg',
  components: {
    ScrollContainer,
  },
  computed: {
    ...mapStores(useCoreToolStore, useOrgStore, usePostStore, useRoleStore),
  },
})
export default class CoreOrg extends Vue {

  orgTreeData: Array<CoreTree<any>> = []

  orgTreeCurrentSelectData: string[] = []

  orgTreeGetDataSet() {
    this.orgTreeData = []
    this.orgTreeCurrentSelectData = []
    this.orgStore.getOrgTreeData(this.$toParam<OrgQO>({ weightOrderAsc: 'y' } as OrgQO)).then((res: Res<CoreTree<any>>) => {
      if (res.succ) {
        this.orgTreeData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.postFormQuerySearch()
  }

  orgTreeChangeSelect(selectedKeys, e) {
    this.postFormQueryModel.fkid = this.orgTreeCurrentSelectData[0]
    this.postFormQuerySearch()
  }

  orgEditDrawerVisible: boolean = false

  orgFormSaveSubmitting: boolean = false

  orgFormSaveContinue: boolean = false

  orgFormModel: Org = toOrg()

  orgFormRules: object = {
    orgName: [
      { type: 'string', required: true, message: '组织机构名称不能为空！', trigger: ['change', 'blur'] },
    ],
    orgNameAbbr: [
      { type: 'string', required: true, message: '组织机构简称不能为空！', trigger: ['change', 'blur'] },
    ],
    orgType: [
      { type: 'string', required: true, message: '组织机构类型不能为空！', trigger: ['change', 'blur'] },
    ],
    // idPath: [
    //   { type: 'string', required: true, message: 'Id路径不能为空！', trigger: ['change', 'blur'] },
    // ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
    // pid: [
    //   { type: 'string', required: true, message: '父级Id不能为空！', trigger: ['change', 'blur'] },
    // ],
  }

  orgPidSelectData: Array<Org> = []

  orgFormReset() {
    if (this.$refs.orgFormRef) {
      this.$refs.orgFormRef.resetFields()
      this.orgFormModel = toOrg()
    }
  }

  orgFormSubmit() {
    this.orgFormSaveSubmitting = true
    this.$refs.orgFormRef.validate().then(() => {
      if (this.orgFormModel.id) {
        this.orgStore.update(this.$toParam<Org>(this.orgFormModel)).then((res: Res<Org>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.orgFormReset()
            this.orgFormSaveSubmitting = false
            if (!this.orgFormSaveContinue) {
              this.orgEditDrawerVisible = false
              this.orgTreeGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.orgFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.orgFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.orgStore.save(this.$toParam<Org>(this.orgFormModel)).then((res: Res<Org>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.orgFormReset()
            this.orgFormSaveSubmitting = false
            if (!this.orgFormSaveContinue) {
              this.orgEditDrawerVisible = false
              this.orgTreeGetDataSet()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.orgFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.orgFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.orgFormSaveSubmitting = false }, 3000)
    })
  }

  orgFormSave() {
    this.orgFormSaveContinue = false
    this.orgFormSubmit()
  }

  orgFormNextAdd() {
    this.orgFormSaveContinue = true
    this.orgFormSubmit()
  }

  orgAdd() {
    if (this.orgFormModel.id) {
      this.orgFormReset()
    }
    this.orgStore.query(this.$toParam<OrgQO>({ weightOrderAsc: 'y' } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.orgPidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.orgFormModel.pid = this.orgTreeCurrentSelectData[0]
    this.orgEditDrawerVisible = true
  }

  orgUpdate() {
    this.orgStore.query(this.$toParam<OrgQO>({ weightOrderAsc: 'y' } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.orgPidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.orgStore.pk(this.$toParam<OrgQO>({ id: this.orgTreeCurrentSelectData[0] } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.orgFormReset()
        this.orgFormModel = { ...res.data[0] }
        this.orgEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  orgEditDrawerOnClose() {
    if (this.orgFormSaveContinue) {
      this.orgFormSaveContinue = false
      this.orgTreeGetDataSet()
    }
  }

  orgDelete() {
    this.orgStore.remove(this.$toParam<Org>({ id: this.orgTreeCurrentSelectData[0] } as Org)).then((res: Res<Org>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.orgTreeGetDataSet()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  orgDetailDrawerVisible: boolean = false

  orgDetailModel: OrgVO = toOrgVO()

  orgDetailInfo() {
    this.orgStore.pkVo(this.$toParam<OrgQO>({ id: this.orgTreeCurrentSelectData[0] } as OrgQO)).then((res: Res<OrgVO>) => {
      if (res.succ) {
        this.orgDetailModel = { ...res.data[0] }
        this.orgDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  orgDetailUpdate() {
    this.orgStore.pk(this.$toParam<OrgQO>({ id: this.orgDetailModel.id } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.orgFormReset()
        this.orgFormModel = { ...res.data[0] }
        this.orgDetailDrawerVisible = false
        this.orgEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  postListDataSet: Array<PostVO> = []

  postListColumns: Array<object> = [
    {
      title: '岗位名称',
      dataIndex: 'postName',
      align: 'center',
      // ellipsis: true,
      // width: 200,
    },
    // {
    //   title: '描述',
    //   dataIndex: 'description',
    //   align: 'center',
    //   // ellipsis: true,
    //   // width: 200,
    // },
    {
      title: '排序权重',
      dataIndex: 'weightOrder',
      align: 'center',
      // ellipsis: true,
      width: 100,
    },
    // {
    //   title: '外键Id',
    //   dataIndex: 'fkid',
    //   align: 'center',
    //   // ellipsis: true,
    //   // width: 200,
    // },
  ]

  postListLoading: boolean = false

  postListSelectedRowKeys: string[] = []

  postListSelectData: Array<PostVO> = []

  postListSelectLength: number = 0

  postListSelectCurrentRowIndex: number = -1

  postListSelectCurrentRowData: PostVO = toPostVO()

  postListPaginationVisible: boolean = true

  postListDataPage: number = 1

  postListDataRows: number = 10

  postListDataCount: number = 0

  postListRowOnChange(selectedRowKeys, selectedRows) {
    this.postListSelectedRowKeys = [ ...selectedRowKeys ]
    this.postListSelectData = [ ...selectedRows ]
    this.postListSelectLength = selectedRows.length
  }

  postListCustomRow(record, index) {
    return {
      'class': index === this.postListSelectCurrentRowIndex ? 'ant-table-row-selected' : '',
      onClick: (event) => {
        this.postListSelectCurrentRowIndex = index
        this.postListSelectCurrentRowData = { ...record }
        let selectedRowKeys = [ ...this.postListSelectedRowKeys ]
        let selectedRows = [ ...this.postListSelectData ]
        if (selectedRowKeys.includes(record.id)) {
          this.postListRowOnChange([ ...selectedRowKeys.filter(eo => { return eo !== record.id }) ], [ ...selectedRows.filter(eo => { return eo.id !== record.id }) ])
        } else {
          selectedRowKeys.push(record.id)
          selectedRows.push(record)
          this.postListRowOnChange([ ...new Set(selectedRowKeys) ], [ ...new Set(selectedRows) ])
        }
      },
      onDblclick: (event) => {
        this.postStore.pkVo(this.$toParam<PostQO>({ id: this.postListSelectCurrentRowData.id } as PostQO)).then((res: Res<PostVO>) => {
          if (res.succ) {
            this.postDetailModel = { ...res.data[0] }
            this.postDetailDrawerVisible = true
          } else {
            this.$message.error(res.msg, 6)
          }
        })
      },
    }
  }

  postListGetDataSet() {
    this.postListLoading = true
    this.postListRowOnChange([], [])
    this.postListSelectCurrentRowIndex = -1
    this.postListSelectCurrentRowData = toPostVO()
    this.postStore.pagingVo(this.$toParam<PostQO>([], this.postListDataPage, this.postListDataRows)).then((res: Res<PostVO>) => {
      if (res.succ) {
        this.postListPaginationVisible = true
        this.postListDataSet = [ ...res.data ]
        this.postListDataCount = res.count
        setTimeout(() => { this.postListLoading = false }, 500)
      } else {
        this.$message.error(res.msg, 6)
        setTimeout(() => { this.postListLoading = false }, 500)
      }
    }).catch(err => {
      setTimeout(() => { this.postListLoading = false }, 500)
    })
  }

  postListPaginationChange() {
    this.postListGetDataSet()
  }

  postListPaginationShowSizeChange() {
    this.postListGetDataSet()
  }

  postFormQueryVisible: boolean = false

  postFormQuerySubmitting: boolean = false

  postFormQueryModel: PostQO = toPostQO()

  postFormQueryUpDown() {
    this.postFormQueryVisible = !this.postFormQueryVisible
  }

  postFormQueryReset() {
    if (this.$refs.postFormQueryRef) {
      this.$refs.postFormQueryRef.resetFields()
      this.postFormQueryModel = toPostQO()
    }
  }

  postFormQuerySearch() {
    this.postFormQuerySubmitting = true
    this.postListLoading = true
    this.postListRowOnChange([], [])
    this.postListSelectCurrentRowIndex = -1
    this.postListSelectCurrentRowData = toPostVO()
    if (this.orgTreeCurrentSelectData[0]) {
      this.postStore.queryVo(this.$toParam<PostQO>(this.$toSearch({ ...this.postFormQueryModel }))).then((res: Res<PostVO>) => {
        if (res.succ) {
          this.postListPaginationVisible = false
          this.postListDataSet = [ ...res.data ]
          this.postListDataCount = res.count
          setTimeout(() => { this.postFormQuerySubmitting = false }, 5000)
          setTimeout(() => { this.postListLoading = false }, 500)
        } else {
          this.$message.error(res.msg, 6)
          setTimeout(() => { this.postFormQuerySubmitting = false }, 5000)
          setTimeout(() => { this.postListLoading = false }, 500)
        }
      }).catch(err => {
        setTimeout(() => { this.postFormQuerySubmitting = false }, 5000)
        setTimeout(() => { this.postListLoading = false }, 500)
      })
    } else {
      this.postListPaginationVisible = false
      this.postListDataSet = []
      this.postListDataCount = 0
      setTimeout(() => { this.postFormQuerySubmitting = false }, 5000)
      setTimeout(() => { this.postListLoading = false }, 500)
    }
  }

  postEditDrawerVisible: boolean = false

  postFormSaveSubmitting: boolean = false

  postFormSaveContinue: boolean = false

  postFormModel: PostVO = toPostVO()

  postFormRules: object = {
    postName: [
      { type: 'string', required: true, message: '岗位名称不能为空！', trigger: ['change', 'blur'] },
    ],
    // description: [
    //   { type: 'string', required: true, message: '描述不能为空！', trigger: ['change', 'blur'] },
    // ],
    // weightOrder: [
    //   { type: 'number', required: true, message: '排序权重不能为空！', trigger: ['change', 'blur'] },
    // ],
    // fkid: [
    //   { type: 'string', required: true, message: '外键Id不能为空！', trigger: ['change', 'blur'] },
    // ],
  }

  postFkidSelectData: Array<Org> = []

  postRoleListSelectData: Array<Role> = []

  postFormReset() {
    if (this.$refs.postFormRef) {
      this.$refs.postFormRef.resetFields()
      this.postFormModel = toPostVO()
      this.postFormModel.fkid = this.orgTreeCurrentSelectData[0]
    }
  }

  postFormSubmit() {
    this.postFormSaveSubmitting = true
    this.$refs.postFormRef.validate().then(() => {
      if (this.postFormModel.id) {
        this.postStore.update(this.$toParam<PostVO>(this.postFormModel)).then((res: Res<PostVO>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.postFormReset()
            this.postFormSaveSubmitting = false
            this.postFormModel.fkid = this.orgTreeCurrentSelectData[0]
            if (!this.postFormSaveContinue) {
              this.postEditDrawerVisible = false
              this.postFormQuerySearch()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.postFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.postFormSaveSubmitting = false }, 3000)
        })
      } else {
        this.postStore.save(this.$toParam<PostVO>(this.postFormModel)).then((res: Res<PostVO>) => {
          if (res.succ) {
            this.$message.success(res.msg, 3)
            this.postFormReset()
            this.postFormSaveSubmitting = false
            this.postFormModel.fkid = this.orgTreeCurrentSelectData[0]
            if (!this.postFormSaveContinue) {
              this.postEditDrawerVisible = false
              this.postFormQuerySearch()
            }
          } else {
            this.$message.error(res.msg, 6)
            setTimeout(() => { this.postFormSaveSubmitting = false }, 3000)
          }
        }).catch(err => {
          setTimeout(() => { this.postFormSaveSubmitting = false }, 3000)
        })
      }
    }).catch(() => {
      setTimeout(() => { this.postFormSaveSubmitting = false }, 3000)
    })
  }

  postFormSave() {
    this.postFormSaveContinue = false
    this.postFormSubmit()
  }

  postFormNextAdd() {
    this.postFormSaveContinue = true
    this.postFormSubmit()
  }

  postAdd() {
    if (this.postFormModel.id) {
      this.postFormReset()
    }
    this.orgStore.query(this.$toParam<OrgQO>({ weightOrderAsc: 'y' } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.postFkidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.roleStore.query(this.$toParam<RoleQO>([])).then((res: Res<Role>) => {
      if (res.succ) {
        this.postRoleListSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.postFormModel.fkid = this.orgTreeCurrentSelectData[0]
    this.postEditDrawerVisible = true
  }

  postUpdate() {
    this.orgStore.query(this.$toParam<OrgQO>({ weightOrderAsc: 'y' } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.postFkidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.roleStore.query(this.$toParam<RoleQO>([])).then((res: Res<Role>) => {
      if (res.succ) {
        this.postRoleListSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.postStore.pkVo(this.$toParam<PostQO>({ id: this.postListSelectData[0].id } as PostQO)).then((res: Res<PostVO>) => {
      if (res.succ) {
        this.postFormReset()
        this.postFormModel = { ...res.data[0] }
        this.postEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  postEditDrawerOnClose() {
    if (this.postFormSaveContinue) {
      this.postFormSaveContinue = false
      this.postFormQuerySearch()
    }
  }

  postDelete() {
    let param: Array<Post> = []
    this.postListSelectData.forEach((eo: PostVO) => {
      param.push({ id: eo.id } as Post)
    })
    this.postStore.removeBatch(this.$toParam<Post>(param)).then((res: Res<Post>) => {
      if (res.succ) {
        this.$message.success(res.msg, 3)
        this.postFormQuerySearch()
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  postDetailDrawerVisible: boolean = false

  postDetailModel: PostVO = toPostVO()

  postDetailInfo() {
    this.postStore.pkVo(this.$toParam<PostQO>({ id: this.postListSelectData[0].id } as PostQO)).then((res: Res<PostVO>) => {
      if (res.succ) {
        this.postDetailModel = { ...res.data[0] }
        this.postDetailDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  postDetailUpdate() {
    this.orgStore.query(this.$toParam<OrgQO>({ weightOrderAsc: 'y' } as OrgQO)).then((res: Res<Org>) => {
      if (res.succ) {
        this.postFkidSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.roleStore.query(this.$toParam<RoleQO>([])).then((res: Res<Role>) => {
      if (res.succ) {
        this.postRoleListSelectData = [ ...res.data ]
      } else {
        this.$message.error(res.msg, 6)
      }
    })
    this.postStore.pkVo(this.$toParam<PostQO>({ id: this.postDetailModel.id } as PostQO)).then((res: Res<PostVO>) => {
      if (res.succ) {
        this.postFormReset()
        this.postFormModel = { ...res.data[0] }
        this.postDetailDrawerVisible = false
        this.postEditDrawerVisible = true
      } else {
        this.$message.error(res.msg, 6)
      }
    })
  }

  mounted() {
    this.postFormQueryModel.weightOrderAsc = 'y'
    this.orgTreeGetDataSet()
  }

}
</script>

<style scoped lang="less">

</style>
